package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.mapper.SysDicMapper;
import com.doubi.model.sys.TSysDic;
import com.doubi.model.sys.TSysDicChild;
import com.doubi.model.sys.TSysUser;
import com.doubi.modelvo.sys.SysDicChildVo;
import com.doubi.utils.DataUtil;
import com.doubi.utils.JsonUtils;
import com.doubi.vo.AppModel;
import com.doubi.vo.AppUiModel;
import com.doubi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysDicService extends ServiceImpl<SysDicMapper, TSysDic> {
    @Autowired
    private SysDicMapper sysDicMapper;
    @Autowired
    private SysDicChildService sysDicChildService;

    /**
     * 功能描述：获取字典父级分页数据
     * @author   MrDoubi 2019-06-28 15:33
     * @param page          Page对象
     * @param sysDic        TSysDic对象
     * @return              返回Page对象
     */
    public PageVo<TSysDic> getPage(PageVo<TSysDic> page, TSysDic sysDic) {
        QueryWrapper<TSysDic> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(sysDic.getParentName())){
            wrapper.like("parent_name",sysDic.getParentName());
        }
        sysDicMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 功能描述：启用禁用字典表父级
     * @author   MrDoubi 2019-06-28 15:40
     * @param sysDic        TSysDic对象
     * @param sysUser       TSysUser对象
     * @return              返回AppModel对象
     */
    @CacheEvict("getChildByParentCode")
    public AppModel updateStatus(TSysDic sysDic, TSysUser sysUser) {
        TSysDic bean = super.getById(sysDic.getOid());
        if (DataUtil.isEmpty(bean)) {
            return new AppModel(-1, "数据不存在！");
        }
        bean.setUpdateBy(sysUser.getOid());
        bean.setUpdateTime(new Date());
        if (bean.getStatus() == 1) {
            bean.setStatus(2);
            sysDicMapper.updateById(bean);
            return new AppModel(0, "禁用成功！");
        } else {
            bean.setStatus(1);
            sysDicMapper.updateById(bean);
            return new AppModel(0, "启用成功！");
        }
    }

    /**
     * 功能描述：删除父级字典表
     * @author   MrDoubi 2019-06-28 15:50
     * @param sysDic        TSysDic对象
     * @return              返回int型结果
     */
    @CacheEvict("getChildByParentCode")
    public Integer delBean(TSysDic sysDic) {
        sysDic = super.getById(sysDic.getOid());
        if (DataUtil.isNotEmpty(sysDic)) {
            sysDicChildService.deleteAllByParentOid(sysDic.getOid());
            sysDicMapper.deleteById(sysDic.getOid());
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 功能描述：新增或修改字典父级
     * @author   MrDoubi 2019-06-28 16:47
     * @param sysDic        TSysDic对象
     * @param sysUser       TSysUser对象
     * @return              返回AppModel对象
     */
    @CacheEvict("getChildByParentCode")
    public AppModel saveOrUpdate(TSysDic sysDic, TSysUser sysUser) {
        if (DataUtil.isNotEmpty(sysDic.getOid())) {
            TSysDic bean = sysDicMapper.selectById(sysDic.getOid());
            if (DataUtil.isEmpty(bean)) {
                return new AppModel(-1, "数据不存在！");
            }
            bean.setAuthority(sysDic.getAuthority());
            bean.setParentCode(sysDic.getParentCode());
            bean.setParentName(sysDic.getParentName());
            bean.setStatus(sysDic.getStatus());
            bean.setRemark(sysDic.getRemark());
            bean.setUpdateBy(sysUser.getOid());
            bean.setUpdateTime(new Date());
            sysDicMapper.updateById(bean);
        } else {
            QueryWrapper<TSysDic> wrapper = new QueryWrapper<>();
            wrapper.eq("parentCode",sysDic.getParentCode());
            if (DataUtil.isNotEmpty(super.getOne(wrapper))) {
                return new AppModel(-1, "字典标识已存在！");
            }
            sysDic.setCreateBy(sysUser.getOid());
            sysDic.setCreateTime(new Date());
            sysDicMapper.insert(sysDic);
        }
        return new AppModel(0, "数据操作成功！");
    }

    /**
     * MrDoubi
     * 2020年3月25日 15:56:15
     * @param code code
     * @return model
     */
    @Cacheable("getChildByParentCode")
    public AppUiModel<List<SysDicChildVo>> getChildByParentCode(String code){
        if(DataUtil.isEmpty(code)){
            return new AppUiModel<>(400,"请选择code");
        }
        QueryWrapper<TSysDicChild> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code",code);
        wrapper.eq("status",1);
        wrapper.orderByAsc("order_by");
        return new AppUiModel<>(JsonUtils.copyList(sysDicChildService.list(wrapper),SysDicChildVo.class));
    }
}
